package listings;

import classes.AdminDB;
import classes.BaseServlet;
import classes.product;
import classes.ObjetoDB;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alvaro
 */
public class listProducts extends BaseServlet {

    RequestDispatcher rd;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
    */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException
        {
            //Sesion
            HttpSession s = request.getSession();
            
            String stats = request.getParameter("stats");
            
            if(stats == null)
                stats = "";
            
            System.out.println("Valor del campo stats: "+stats);
            AdminDB adb = new AdminDB();
            product cl = new product();
            
            //Creacion de la lista
            List<product> productos = null;

                

            //Metemos en la lista de productos todos los productos obtenidos en la busqueda
            productos = cl.getAllProductos(adb.getConnection());
              
            System.out.println("Se ha recibido una peticion para listar productos");
            //Metemos en la sesion la lista
            s.setAttribute("listaDeproductos", productos);
            //Redirigimos a statics si se nos pide desde las estadisticas
            if(stats.equals("stats"))
            {
                response.sendRedirect("Admin/statistics.jsp");
            }
            else
            {
            //Sino redirigimos a la lista de todos los productos
            response.sendRedirect("allproducts.jsp");
            }
        
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(listProducts.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(listProducts.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(listProducts.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(listProducts.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
